<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>刮刮乐</title>
</head>

<body>
    <div style="position: relative;">
        <h1 style="position: absolute; top: 10px; left: 50px; z-index: -1;">特等奖</h1>
        <canvas id="myCanvas" width="200" height="100"></canvas>
    </div>

    <script>
        let canvas = document.getElementById("myCanvas");
        let ctx = canvas.getContext("2d");
        let isClick = false;

        // 绘制灰色图层
        ctx.fillStyle = "#ccc";
        ctx.fillRect(0, 0, canvas.width, canvas.height);

        ctx.lineCap = "round";
        ctx.lineJoin = "round";
        ctx.lineWidth = 20;

        // 鼠标按下
        canvas.addEventListener("mousedown", function (e) {
            let x = e.offsetX;
            let y = e.offsetY;
            ctx.beginPath();
            // 设置绘制的起点为当前点击的位置
            ctx.moveTo(x, y);
            isClick = true;
        })

        // 鼠标移动
        canvas.addEventListener("mousemove", function (e) {
            if (!isClick) {
                return;
            }
            let x = e.offsetX;
            let y = e.offsetY;
            // 绘制的内容与原矩形重合部分清空
            ctx.globalCompositeOperation = "destination-out";
            ctx.lineTo(x, y);
            ctx.stroke();
        })

        // 鼠标松开
        canvas.addEventListener("mouseup", function (e) {
            isClick = false;
        })
    </script>
</body>

</html>